package com.sfh.cp.base;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sfh.cp.R;
import com.sfh.cp.widget.refresh.SwipeRefreshMoreLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import crm.sfh.server.entity.rsp.TPage;

/**
 * Created by Administrator on 2018/3/7.
 */

public abstract class RefreshRecyleFragment extends AbstractBaseFragment implements BaseQuickAdapter.OnItemChildClickListener,
        BaseQuickAdapter.OnItemClickListener, SwipeRefreshMoreLayout.OnLoadingListener {
    @BindView(R.id.refreshLayout)
    public SwipeRefreshMoreLayout refreshLayout;

    public abstract BaseQuickAdapter createAdapter();

    public abstract boolean loadMore();

    protected BaseQuickAdapter adapter;

    @Override
    public int getLayout() {
        return R.layout.base_refresh_fragment;
    }


    @Override
    public void initData(View view) {
        super.initData(view);
         ButterKnife.bind(this, view);
        refreshLayout.addItemDecorationSize(R.dimen.y15);
        adapter = createAdapter();
        this.refreshLayout.setAdapter(adapter, loadMore());
        this.refreshLayout.setOnItemClickListener(this);
        this.refreshLayout.setOnItemChildClickListener(this);
        this.refreshLayout.setOnLoadingListener(this);
        this.refreshLayout.onAutoRefresh();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onLoadMoreUI() {

    }

   protected int nowPage = 1;

    public <T> void success(TPage<T> data) {

        this.nowPage = data.getNowPage();
        this.refreshLayout.onRefreshCompale(data.getList(), data.getNowPage() != 1);
        if (this.nowPage >= data.getTotalPage()) {
            this.refreshLayout.onLoadAllCompale();
        }
    }

    public void error(String code, String error) {

        this.refreshLayout.onLoadMoreFail();
        this.showToast(error + code);
    }

}
